//! 调试存储系统的测试
//! 用于验证INSERT和数据扫描功能是否正常工作

use cdb::{Database, Result, Error};
use cdb::config::DatabaseConfig;
use tempfile::TempDir;

/// 创建测试数据库
async fn create_test_database() -> Result<(Database, TempDir)> {
    let temp_dir = TempDir::new()
        .map_err(|e| Error::storage(format!("创建临时目录失败: {}", e)))?;
    
    let mut config = DatabaseConfig::default();
    config.enable_persistence = true;
    config.data_directory = Some(temp_dir.path().to_path_buf());
    
    let db = Database::with_config(config).await?;
    Ok((db, temp_dir))
}

#[tokio::test]
async fn test_insert_and_scan() {
    let (db, _temp_dir) = create_test_database().await.unwrap();
    
    let mut conn = db.connect().await.unwrap();
    
    // 创建测试表
    println!("创建测试表...");
    conn.execute("CREATE TABLE test_users (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        age INTEGER
    )").await.unwrap();
    
    // 插入测试数据
    println!("插入测试数据...");
    let result1 = conn.execute("INSERT INTO test_users VALUES (1, 'Alice', 25)").await.unwrap();
    println!("INSERT结果1: affected_rows = {}", result1.get_affected_rows());
    
    let result2 = conn.execute("INSERT INTO test_users VALUES (2, 'Bob', 30)").await.unwrap();
    println!("INSERT结果2: affected_rows = {}", result2.get_affected_rows());
    
    // 尝试查询数据
    println!("查询数据...");
    let query_result = conn.execute("SELECT * FROM test_users").await.unwrap();
    println!("SELECT结果: rows = {}", query_result.rows().len());
    
    for (i, row) in query_result.rows().iter().enumerate() {
        println!("行 {}: {:?}", i, row);
    }
    
    // 现在测试UPDATE操作
    println!("尝试UPDATE操作...");
    let update_result = conn.execute("UPDATE test_users SET age = 26 WHERE name = 'Alice'").await.unwrap();
    println!("UPDATE结果: affected_rows = {}", update_result.get_affected_rows());

    // 再次查询验证
    println!("UPDATE后查询数据...");
    let query_result2 = conn.execute("SELECT * FROM test_users").await.unwrap();
    println!("UPDATE后SELECT结果: rows = {}", query_result2.rows().len());

    for (i, row) in query_result2.rows().iter().enumerate() {
        println!("UPDATE后行 {}: {:?}", i, row);
    }

    // 现在测试DELETE操作
    println!("尝试DELETE操作...");
    let delete_result = conn.execute("DELETE FROM test_users WHERE name = 'Bob'").await.unwrap();
    println!("DELETE结果: affected_rows = {}", delete_result.get_affected_rows());

    // 再次查询验证
    println!("DELETE后查询数据...");
    let query_result3 = conn.execute("SELECT * FROM test_users").await.unwrap();
    println!("DELETE后SELECT结果: rows = {}", query_result3.rows().len());

    for (i, row) in query_result3.rows().iter().enumerate() {
        println!("DELETE后行 {}: {:?}", i, row);
    }

    println!("✅ 基本的CREATE、INSERT、SELECT、UPDATE、DELETE操作都正常工作！");
}

#[tokio::test]
async fn test_delete_operation() {
    let (db, _temp_dir) = create_test_database().await.unwrap();
    
    let mut conn = db.connect().await.unwrap();
    
    // 创建测试表
    println!("创建测试表...");
    conn.execute("CREATE TABLE test_users2 (
        id INTEGER PRIMARY KEY,
        name TEXT NOT NULL,
        age INTEGER
    )").await.unwrap();
    
    // 插入测试数据
    println!("插入测试数据...");
    conn.execute("INSERT INTO test_users2 VALUES (1, 'Alice', 25)").await.unwrap();
    conn.execute("INSERT INTO test_users2 VALUES (2, 'Bob', 30)").await.unwrap();
    conn.execute("INSERT INTO test_users2 VALUES (3, 'Charlie', 35)").await.unwrap();
    
    // 查询初始数据
    println!("查询初始数据...");
    let query_result = conn.execute("SELECT * FROM test_users2").await.unwrap();
    println!("初始SELECT结果: rows = {}", query_result.rows().len());
    
    // 尝试DELETE
    println!("尝试DELETE操作...");
    let delete_result = conn.execute("DELETE FROM test_users2 WHERE name = 'Bob'").await.unwrap();
    println!("DELETE结果: affected_rows = {}", delete_result.get_affected_rows());
    
    // 再次查询验证
    println!("DELETE后查询数据...");
    let query_result2 = conn.execute("SELECT * FROM test_users2").await.unwrap();
    println!("DELETE后SELECT结果: rows = {}", query_result2.rows().len());
    
    for (i, row) in query_result2.rows().iter().enumerate() {
        println!("DELETE后行 {}: {:?}", i, row);
    }
}
